package com.stjb.furns.dao.impl;

import com.stjb.furns.dao.BasicDAO;
import com.stjb.furns.dao.MemberDAO;
import com.stjb.furns.entity.Member;

import java.util.Arrays;

/**
 * @Author: situ
 * @Date: 2025/5/25 20:42
 * @Description:
 **/
public class MemberDaoImpl extends BasicDAO<Member> implements MemberDAO {

    /**
     * 通过用户名返回Member
     * @param username 用户名
     * @return 对应的Member 没有则返回空
     */

    @Override
    public Member queryMemberByUsername(String username) {

        String sql = "SELECT `id`,`username`,`password`,`email` FROM member " +
                "where `username`=?";

//        Member member = querySingle(sql, Member.class, username);
        Member member = querySingle(sql, Member.class, username);
        return member;
    }

    /**
     * 保存一个会员
     * @param member 传入Member对象
     * @return 返回-1失败 返回1成功
     */

    @Override
    public int saveMember(Member member) {
        String sql = "INSERT INTO `member` (`username`,`password`,`email`) " +
                "VALUES(?,MD5(?),?)";
        return update(sql,member.getUsername(),member.getPassword(),member.getEmail());
    }

    /**
     * 通过用户名和密码返回一个对象
     * @param username 用户名
     * @param password 密码
     * @return 可以登录则返回对象
     */
    @Override
    public Member queryMemberByUsernameAndPassword(String username,String password) {
        String sql = "SELECT * FROM member \n" +
                "WHERE `username`=? AND `password`=MD5(?)";
        return querySingle(sql,Member.class,username,password);
    }


}
